Imports System.Data
Partial Class GTSWeb_Carrier_Manage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        pBase.Visible = False
        pIncremental.Visible = False
        pFixed.Visible = False
        Dim RateLogic As New GTSSystem.BLL.Rates()
        Dim MyData As DataTable
        Dim MyTable As New DataTable("ContractRates")
        MyTable.Columns.Add("RateID", GetType(String))
        MyTable.Columns.Add("Carrier", GetType(String))
        MyTable.Columns.Add("Origin", GetType(String))
        MyTable.Columns.Add("Destination", GetType(String))
        MyTable.Constraints.Add("Key1", MyTable.Columns(0), True)
        Dim user As MembershipUser = Membership.GetUser
        Dim p As ProfileCommon = ProfileCommon.Create(user.UserName)
        Try
            MyData = RateLogic.GetRateByBranchID(p.BranchId)
            If MyData.Rows.Count > 0 Then
                For n As Integer = 0 To MyData.Rows.Count - 1
                    Dim RateID As Integer = MyData.Rows(n).Item("RateID")
                    Dim ContractID As Integer = MyData.Rows(n).Item("ContractID")
                    Dim Origin As Integer = MyData.Rows(n).Item("Origin")
                    Dim Destination As Integer = MyData.Rows(n).Item("Destination")
                    Dim OData As DataTable
                    Dim DData As DataTable
                    Dim CData As DataTable
                    Dim ContractInfoLogic As New GTSSystem.BLL.Contract()
                    CData = ContractInfoLogic.GetContractInfoByContractID(ContractID)
                    Dim CarrierName As String = CData.Rows(0).Item("CarrierName")
                    Dim LocationLogic As New GTSSystem.BLL.RateLocation()
                    OData = LocationLogic.GetRateLocationByRateLocationID(Origin)
                    DData = LocationLogic.GetRateLocationByRateLocationID(Destination)
                    Dim OCity As String = OData.Rows(0).Item("City")
                    Dim OProv As String = OData.Rows(0).Item("Province")
                    Dim DCity As String = DData.Rows(0).Item("City")
                    Dim DProv As String = DData.Rows(0).Item("Province")
                    Dim aRow As DataRow
                    aRow = MyTable.NewRow
                    aRow.Item("RateID") = RateID
                    aRow.Item("Carrier") = CarrierName
                    aRow.Item("Origin") = OCity + ", " + OProv
                    aRow.Item("Destination") = DCity + ", " + DProv
                    MyTable.Rows.Add(aRow)
                Next
                gdvwRateID.DataSource = MyTable
                gdvwRateID.DataBind()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub linkAddFixed_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
                                    Handles linkAddFixed.Click
        If txtFMinWeight.Text.Trim = "" Or txtFMaxWeight.Text.Trim = "" Or txtFRateCharge.Text.Trim = "" Then
            FMessage.text = "Missing required field"
        Else
            Try
                Dim RateLogic As New GTSSystem.BLL.Rates
                RateLogic.AddFixedRateDetail(gdvwRateID.SelectedValue, radServiceType.SelectedValue, ddlRateType.SelectedValue, _
                                        txtFMinWeight.Text, txtFMaxWeight.Text, txtFRateCharge.Text)
                Clear()
                Message.Text = "Rate detail added successfully"
                ddlRateType.SelectedIndex = -1
                gdvwRateDetail.DataBind()
            Catch ex As Exception
                Message.Text = "Unable to add rate detail"
                ddlRateType.SelectedIndex = -1
            End Try
        End If
    End Sub

    Protected Sub linkAddIncremental_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
                                            Handles linkAddIncremental.Click
        If txtIMinWeight.Text.Trim = "" Or txtIMaxWeight.Text.Trim = "" Or txtIRateCharge.Text.Trim = "" Or txtIMinCharge.Text.Trim = "" Then
            IMessage.Text = "Missing required field"
        Else
            Try
                Dim RateLogic As New GTSSystem.BLL.Rates
                RateLogic.AddIncrementalRateDetail(gdvwRateID.SelectedValue, radServiceType.SelectedValue, ddlRateType.SelectedValue, _
                                        txtIMinWeight.Text, txtIMaxWeight.Text, txtIRateCharge.Text, txtIMinCharge.Text)
                Clear()
                Message.Text = "Rate detail added successfully"
                ddlRateType.SelectedIndex = -1
                gdvwRateDetail.DataBind()
            Catch ex As Exception
                Message.Text = "Unable to add rate detail"
                ddlRateType.SelectedIndex = -1
            End Try
        End If
    End Sub

    Protected Sub linkAddBase_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
                                    Handles linkAddBase.Click
        If txtBMinWeight.Text.Trim = "" Or txtBMaxWeight.Text.Trim = "" Or txtBRateCharge.Text.Trim = "" Or txtBBaseRate.Text.Trim = "" Then
            BMessage.Text = "Missing required field"
        Else
            Try
                Dim RateLogic As New GTSSystem.BLL.Rates
                RateLogic.AddBaseRateDetail(gdvwRateID.SelectedValue, radServiceType.SelectedValue, ddlRateType.SelectedValue, _
                                        txtBMinWeight.Text, txtBMaxWeight.Text, txtBRateCharge.Text, txtBBaseRate.Text)
                Clear()
                Message.Text = "Rate detail added successfully"
                ddlRateType.SelectedIndex = -1
                gdvwRateDetail.DataBind()
            Catch ex As Exception
                Message.Text = "Unable to add rate detail"
                ddlRateType.SelectedIndex = -1
            End Try
        End If
    End Sub
    Public Sub Clear()
        txtBMinWeight.Text = ""
        txtFMinWeight.Text = ""
        txtIMinWeight.Text = ""
        txtBMaxWeight.Text = ""
        txtFMaxWeight.Text = ""
        txtIMaxWeight.Text = ""
        txtBRateCharge.Text = ""
        txtFRateCharge.Text = ""
        txtIRateCharge.Text = ""
        txtBBaseRate.Text = ""
        txtIMinCharge.Text = ""
        Message.Text = ""
    End Sub

    Protected Sub ddlRateType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
                                                    Handles ddlRateType.SelectedIndexChanged

        If ddlRateType.SelectedIndex = "-1" Then
            pRateDetail.Visible = True
            pBase.Visible = False
            pIncremental.Visible = False
            pFixed.Visible = False
        ElseIf ddlRateType.SelectedIndex = "1" Then
            pRateDetail.Visible = True
            pBase.Visible = True
            pIncremental.Visible = False
            pFixed.Visible = False

        ElseIf ddlRateType.SelectedIndex = "2" Then
            pRateDetail.Visible = True
            pBase.Visible = False
            pIncremental.Visible = False
            pFixed.Visible = True

        ElseIf ddlRateType.SelectedIndex = "3" Then
            pRateDetail.Visible = True
            pBase.Visible = False
            pIncremental.Visible = True
            pFixed.Visible = False



        End If
    End Sub


    Protected Sub gdvwRateID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
                                                    Handles gdvwRateID.SelectedIndexChanged
        pRateDetail.Visible = True
    End Sub
End Class
